
#include <iostream>
#include <fstream>
#include <vector>
#include <dirent.h>
#include <opencv2/opencv.hpp>



std::vector<float> loadData(const std::string& file)
{
    // std::cout << file << std::endl;
    std::ifstream dataFile(file, std::ios::binary);

    if (!dataFile.is_open())
    {
        std::cerr << "Error opening file: " << file << std::endl;
            exit(EXIT_FAILURE);
    }

        //get length of file:
    //   unsigned int len = 0;
    dataFile.seekg (0, std::ios::end);
    size_t len = dataFile.tellg();
    //   std::cout << len << std::endl;
    dataFile.seekg (0, std::ios::beg);

    
    //read data as a block:
    std::vector<float> buffer(len / sizeof(float));
    dataFile.read(reinterpret_cast<char*>(buffer.data()), len);
    dataFile.close();
    return buffer; 


}

std::vector<std::string> listFilesInDirectory(const std::string& directoryPath) {
    std::vector<std::string> files;

    DIR* directory = opendir(directoryPath.c_str());
    if (directory != nullptr) {
        dirent* entry;
        while ((entry = readdir(directory)) != nullptr) {
            if (entry->d_type == DT_REG) { // 判断是否为普通文件
                files.push_back(entry->d_name);
            }
        }
        closedir(directory);
    }

    return files;
}

cv::Mat loadImage(std::string imageFile){
    cv::Mat image = cv::imread(imageFile, cv::IMREAD_COLOR);
    if (image.empty()) {
        std::cerr << "Failed to load image: " << imageFile << std::endl;
    }
    // convertImage(image, inputData, channels, height, width); //convert to sequence

    return image;
}

std::vector<std::vector<float>> loadCalib(std::string calibpath){
    std::vector<std::vector<float>> matrixs;
    std::fstream file(calibpath);
    if (!file.is_open()) {
        std::cerr << "Unable to open file." << std::endl;
        return matrixs;
    }
    
    std::string line;
    while (std::getline(file, line)) {
        std::vector<float> row;
        std::stringstream iss(line);
        std::string cell;
        bool first = true;
        while (std::getline(iss, cell, ' ')) {
            if (first){
                first = false;
            }
            else{
                // float cc = ;
                // std::cout << cc << std::endl;
                row.push_back(std::stof(cell));
            }

        }
        matrixs.push_back(row);
    }


    file.close();
    return matrixs;
}
